<?php echo $html->css("screen/staff_parts.css");?>
<?php echo $html->css("screen/swfupload.css");?>
<?php echo $javascript->link("lib/swfupload/swfupload.js");?>
<?php echo $javascript->link("lib/swfupload/swfupload.queue.js");?>
<?php echo $javascript->link("lib/swfupload/fileprogress.js");?>
<?php echo $javascript->link("lib/swfupload/handlers.js");?>
<?php echo $this->element('menu', array("active" => "2.b"));?>
<ul class="breadcrumb">
<li>
  <?php echo $this->Html->link(__('Shop  ', true), array('action' => 'index', 'controller' => 'parts'), array('class'=>'fist_link'));?>
</li>
<li>          
  <?php echo $this->Html->link(__('Categories  ', true), array('action' => 'index'));?>
</li>
</ul>
<script>
var MAX_FILE=4;
var PATHUPLOAD="<?php echo $this->webroot;?>files/uploads/<?php echo $pathupload;?>/";
var FOLDERUPLOAD="<?php echo $pathupload;?>/";
var FILEUPLOAD="<?php echo $fileupload;?>";
var swfu;
var uploads = new Array();
function cancelUpload(id,fname){
	$("#"+id).hide();
	deleteUploadfile(fname);	
}
function fileQueued(file) {
	try {
		var progress = new FileProgress(file, this.customSettings.progressTarget);		
			progress.setStatus("Pending...");
			progress.toggleCancel(true, this);		
	} catch (ex) {
		this.debug(ex);
	}

}
function uploadSuccess1(file, serverData) {
	try {		
		fileuploaded = FILEUPLOAD+"_"+file.index+file.type;		
		//$("#thumbnail").html('<a class="upload_close" href="#" target="_self" onclick="cancelUpload();return false;">&nbsp;&nbsp;</a><img src="'+PATHUPLOAD+fileuploaded+'" class="thumbnail" />');		
		//progress.toggleCancel(false);
		var progress = new FileProgress(file, this.customSettings.progressTarget);
		progress.setStatus("Done");	
		$("#"+file.id+">div>.progressName").html('<a class="upload_close" href="#" target="_self" onclick="cancelUpload(\''+file.id+'\',\''+fileuploaded+'\');return false;">&nbsp;&nbsp;</a><img src="'+PATHUPLOAD+fileuploaded+'" class="thumbnail" />');
		uploads[uploads.length]=fileuploaded;
		write2input();
	} catch (ex) {
		alert(ex);
	}
}
function uploadStart1(file) {
	try {
		if (!uploadable()){		
			alert("Max file upload is "+MAX_FILE);
			swfu.cancelUpload(file.id,null);
		}
		this.addPostParam("path", FOLDERUPLOAD);
		this.addPostParam("file", FILEUPLOAD+"_"+file.index);		
		var progress = new FileProgress(file, this.customSettings.progressTarget);		
		progress.toggleCancel(true, this);
		progress.setStatus("Uploading...");		
	}
	catch (ex) {}
	
	return true;
}
function uploadProgress1(file, bytesLoaded, bytesTotal) {
	try {		
		var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);
		var progress = new FileProgress(file, this.customSettings.progressTarget);
		progress.setProgress(percent);
		progress.setStatus("Uploading...");
	} catch (ex) {
		this.debug(ex);
	}
}

var settings= {
	flash_url : "<?php echo $this->webroot;?>js/lib/swfupload/swfupload.swf",
	upload_url: "<?php echo $this->webroot;?>files/swfupload.php",
	post_params: {"PHPSESSID" : "<?php echo session_id(); ?>"},
	post_params: {"PHPSESSID" : "<?php echo session_id(); ?>"},

	// File Upload Settings
	file_size_limit : "500",	// 200 kb
	file_types : "*.jpg;*.gif;*.png",
	file_types_description : "Image Files",				
	file_upload_limit : 10,
	file_queue_limit : MAX_FILE,
	custom_settings : {
		progressTarget : "fsUploadProgress",
		cancelButtonId : "btnCancel"
	},
	debug: false,

	// Button settings
	button_image_url: "<?php echo $this->webroot;?>js/lib/swfupload/bg.png",
	button_text : "<span class=\"upload_text\">Upload photo</span>",
	button_text_style : ".upload_text { color:#FFFFFF;font-size:12px;text-align:center;}", 
	button_text_top_padding : 6, 
	button_class: "upload_button",				
	button_placeholder_id : "spanButtonPlaceHolder",
	button_width: 120,
	button_height: 28,
	
	// The event handler functions are defined in handlers.js
	file_queued_handler : fileQueued,
	file_queue_error_handler : fileQueueError,
	file_dialog_complete_handler : fileDialogComplete,
	upload_start_handler : uploadStart1,
	upload_progress_handler : uploadProgress,
	upload_error_handler : uploadError,
	upload_success_handler : uploadSuccess1,
	upload_complete_handler : uploadComplete,
	queue_complete_handler : queueComplete	// Queue plugin event
};

function write2input(){
	var i;	
	for(i=0;i<uploads.length;i++){
		$("#PartPhoto"+(eval(i)+1)).val(uploads[i]);
	}
	for(i=uploads.length;i<MAX_FILE;i++){
		$("#PartPhoto"+(eval(i)+1)).val('');
	}	
}
function deleteUploadfile(id){	
	var i, found = false;	
	if (uploads[uploads.length-1] != id){
		for(i=0;i<uploads.length;i++){
			if(found){
				uploads[i-1]=uploads[i];
			}else if (uploads[i]==id){
				found=true;
				uploads[i]=uploads[i];
			}	
		}
	}
	uploads.length=uploads.length-1;	
	write2input();
}
function uploadable(){
	return uploads.length < MAX_FILE;
}
function loadphotos(){
	var i, image;	
	for(i=0;i<MAX_FILE;i++){
		if($("#PartPhoto"+(eval(i)+1)).val()!=""){
			image = $("#PartPhoto"+(eval(i)+1)).val();
			uploads[i]=image;			
			$("#fsUploadProgress").html($("#fsUploadProgress").html()+'<div class="progressWrapper" id="xSWFUpload_0_'+i+'"><div class="progressContainer"><div class="progressName"><a onclick="cancelUpload(\'xSWFUpload_0_'+i+'\',\''+image+'\');return false;" target="_self" href="#" class="upload_close">&nbsp;&nbsp;</a><img class="thumbnail" src="<?php echo $this->webroot;?>files/uploads/categories/'+image+'"></div></div>')
		}
		else break;
	}	
}
</script>
<script>
	$(document).ready(function(){		
		$("#PartAdminEditForm").validate({
			rules: {
				'data[Part][name]': {
					required: true,
					minlength: 3
				},
				'data[Part][price]': {
					required: true,
					number: true
				},
				'data[Part][photo1]': {
					required: true
				}
			},
			messages: {			
				'data[Part][name]': {
					required: "Please enter a Product's title",
					minlength: "This title must consist of at least 3 characters"
				},		
				'data[Part][price]': {
					required: "Please enter a Product's price",
					number: "Please enter a number (dollar)"
				},		
				'data[Part][photo1]': {
					required: "Please enter a photo",
					number: "Please enter a number (dollar)"
				}
			}
		});
		$('.jqTransform').jqTransform({width:100});		
		swfu = new SWFUpload(settings);  
		loadphotos();
	});
	
</script>	  
<div id="content" class="parts form">
<?php echo $this->Form->create('Part');?>
	<table>
	<tr><td colspan="2">
	
	<?php
		echo $this->Form->input('id');
		echo $this->Form->input('name');
		echo '<span class="jqTransform higher1">';		
		echo $this->Form->input('part_category_id');
		echo '</span>';
		echo $this->Form->input('price');
		echo '<span class="jqTransform">';
		echo $this->Form->input('part_manufacturer_id',array('label'=>'Make'));
		echo '</span>';
		//echo $this->Form->input('item_code');
		//echo $this->Form->input('description');
		//echo $this->Form->input('option');
		//echo $this->Form->input('quantity');
		echo $this->Form->input('photo1',array('type'=>'hidden'));
		echo $this->Form->input('photo2',array('type'=>'hidden'));
		echo $this->Form->input('photo3',array('type'=>'hidden'));
		echo $this->Form->input('photo4',array('type'=>'hidden'));
	?>
	</td></tr>
	<tr><td>
	<label class="upload" for="PartCategoryPhoto">Photo</label>
	</td><td>
	<!--upload-->
		<div id="thumbnails">
			<div id="thumbnail"></div>
		</div>
	<!--upload-->
		<div id="divStatus" style="display:none;">0 Files Uploaded</div>
		<div class="fieldset flash" id="fsUploadProgress">
			<span class="legend"></span>
		</div>
	</td></tr>
	<tr><td></td><td width="100%">
		<div id="upload">
			<span id="spanButtonPlaceHolder"></span>
			<input id="btnCancel" type="button" value="Cancel All Uploads" onclick="swfu.cancelQueue();" disabled="disabled" style="margin-left: 2px; font-size: 8pt; height: 29px; display:none;" />
		</div>
	</td></tr>
		<tr><td></td><td>
<div class="clear">
		<?php echo $form->submit(__('Save', true),array('class'=>'bt','div'=>false));?>
	</div>	 
	</td></tr>
	</table>	
</div>